/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.andersen.web.security;

import com.andersen.database.entity.EmployeeEntity;
import com.andersen.database.entity.UserEntity;
import com.andersen.web.service.CustomUserDetailsService;
import com.andersen.web.service.EmployeeDetailsService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 *
 * @author Corvus 11
 */
@Component
public class AuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{

    @Autowired
    CustomUserDetailsService userService;
    
    @Autowired
    EmployeeDetailsService employeeService;
    
    private static final Logger LOG = Logger.getLogger(AuthSuccessHandler.class);
    
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
                                        throws IOException, ServletException {
        super.onAuthenticationSuccess(request, response, authentication);
        String login = authentication.getName();
        LOG.info("Auth login = " + login);
        
        UserEntity user = userService.getUser(login);
        EmployeeEntity empl = employeeService.getEmployee(user.getEmployeeId());
        String accInitials = empl.getName() + " " + empl.getSurname();
        
        request.getSession().setAttribute("accInitials", accInitials);
    }
    
    
    
}
